package com.eims.ball.info.fragment;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.MySingleton;
import com.android.volley.toolbox.StringXmlRequest;
import com.contans.view.FenxiJishuVo;
import com.eims.ball.info.adapter.FanxiJishuShowAdapter;
import com.eims.sls_app.view.MyListView2;
import com.eims.sls_app.view.MyToast;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;
import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class FenxiJishuFragment extends Fragment {
	private Activity activity;
	private  MyListView2 listviewJishu;
	private FanxiJishuShowAdapter adapter;

	
	private String infoId, hid, gid, mainName, GuesName;

	private MyHandler handler;

	FenxiJishuVo fenxiJishuVo;

	public FenxiJishuFragment() {
		super();
	}



	public static FenxiJishuFragment newInstance(String infoId, String hid, String gid, String mainName, String GuesName) {

		Bundle args = new Bundle();

		FenxiJishuFragment fragment = new FenxiJishuFragment();

		args.putString("infoId",infoId);
		args.putString("hid",hid);
		args.putString("gid",gid);
		args.putString("mainName",mainName);
		args.putString("GuesName",GuesName);



		fragment.setArguments(args);
		return fragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fenxi_jishu_info, container, false);
		activity = getActivity();
		Bundle args = getArguments();
		if (args != null) {
			this.infoId = args.getString("infoId");
			this.hid= args.getString("hid");
			this.gid = args.getString("gid");
			this.mainName= args.getString("mainName");
			this.GuesName= args.getString("GuesName");

		}

		handler = new MyHandler();
		init(view);
		getBalllInfo(infoId);

		return view;
	}

	private void init(View view) {
	
		listviewJishu=(MyListView2) view.findViewById(R.id.listview_jishu);
		

	}

	public void getBalllInfo(String id) {
		MySingleton mySingleton = MySingleton.getInstance(activity);
		ProgressDialogBar.startProgress(activity, "", true);
		StringXmlRequest jsonObjectRequest = new StringXmlRequest(Request.Method.GET,
				"http://01link.igowu123.com/qtjsbf/jc/teamtech/" + id + ".xml", new Response.Listener<String>() {
					@Override
					public void onResponse(String tn) {

						// Log.i("RealTime", "request--------success------" + tn);

						try {
							JSONObject item = XML.toJSONObject(tn);

							Log.e("JSON", item.toString());

							String resultRep = item.optString("rows");

							item = new JSONObject(resultRep);
							
							fenxiJishuVo=new FenxiJishuVo();
							
							fenxiJishuVo.setTitle(item.getString("title"));

							String CreditinfoStr = item.getString("data");

							if (CreditinfoStr != null && !"".equals(CreditinfoStr)) {
								JSONObject array2 = new JSONObject(CreditinfoStr);

								

								fenxiJishuVo.setTeam_A(array2.optString("team_A"));
								fenxiJishuVo.setTeam_B(array2.optString("team_B"));
							

							}

							

							handler.sendEmptyMessage(1);

							//
						} catch (JSONException e) {
							Log.e("JSON", "xml->json失败" + e.getLocalizedMessage());
//							MyToast.getToast(activity, "获取失败").show();

						}
						ProgressDialogBar.stopProgress();
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Log.i("RealTime", "request--------success------" + error);
						MyToast.getToast(activity, "获取失败").show();
						ProgressDialogBar.stopProgress();
					}

				}) {

			@Override
			protected Map<String, String> getParams() throws AuthFailureError {

				Map<String, String> params = new HashMap<String, String>();
				// params.put("appid", "0af5281058488ec1784e433f69d45d81");
				// params.put("custNo", "gl00014156");
				// params.put("orderId", attach);

				return params;
			}
		};

		jsonObjectRequest.setShouldCache(false);// 是否设置缓存

		mySingleton.addToRequestQueue(jsonObjectRequest);

	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:
				
				break;
			case 1:
				WindowManager wm = activity.getWindowManager();
				int width = wm.getDefaultDisplay().getWidth();//获取屏幕宽度
				adapter=new FanxiJishuShowAdapter(activity,width,fenxiJishuVo);
				listviewJishu.setAdapter(adapter);
				
				break;
			default:
				Toast.makeText(getActivity(), "网络异常，加载失败。", Toast.LENGTH_SHORT).show();
				break;
			}
			super.handleMessage(msg);
		}
	}


}
